//
//  FireDropObject.m
//  TowerDefence
//
//  Created by k3 on 6/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FireDropObject.h"
#import "PlayerData.h"
#import "PlayScreen.h"
#import "iCoreGUIController.h"

@implementation FireDropObject

-(id)initFireDropObjectWith:(GameLayer *)_gameLayer {
    self = [super initGameObjectWith:_gameLayer];
    spriteBody = [[CCSprite alloc]init];
    fireDamage = 70;
    return self;
}

-(void)loadFireDropObject {
    CCAnimation *animation = [handler.mainGUIController loadAnimationFromCacheByName:@"fire_drop_effect" withCount:17 delay:0.025];
    
    [spriteBody setRotation:20.0f];
    
    CCCallFunc *function = [CCCallFunc actionWithTarget:self selector:@selector(burnTarget)];
    
    CGPoint targetPoint = [((Enemy *)target) getPositionAfterTime:0.75f];
    targetPoint = ccp(targetPoint.x, targetPoint.y + target.spriteBody.contentSize.height/2);
    [spriteBody runAction:[CCSequence actions:[CCMoveTo actionWithDuration:0.75f position:targetPoint], [CCAnimate actionWithAnimation:animation], function, nil]];
    //[spriteBody runAction:[CCSequence actions:[CCMoveTo actionWithDuration:0.75f position:ccp(350,300)],[CCAnimate actionWithAnimation:animation], nil]];
}
-(void)loadFireEffect {

}

-(void)dropToTarget:(GameObject *)_target {
    target = _target;
    [self moveAnimationWithFrameName:@"fire_drop" frameCount:5];
    [self loadFireDropObject];
}

- (float)randomFloatBetween:(float)smallNumber and:(float)bigNumber {
    float diff = bigNumber - smallNumber;
    return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;
}

-(void)burnTarget {
    /*if(!handler.mainGUIController.isMuteSound) {
        [audioPlayer playWithFileName:AUDIO_EXPLOSIVE loop:NO];
    }*/
    
    [self.spriteBody stopAllActions];
    
    if([handler.listGameObject containsObject:target]){        
        ((Enemy *) target).HP -= fireDamage;
        [((Enemy *) target) updateBloodMeter];
        [((Enemy *) target) animationHit];
        
        /*
        if(((Enemy *) target).HP <= 0) {
            [((Enemy *) target) animationDie];
        }*/
    }
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    [self.spriteBody removeFromParentAndCleanup:YES];
    [handler removeGameObject:self];
}

-(void)moveAnimationWithFrameName:(NSString *)_frameName frameCount:(int)_frameCount {
    CCAnimation *animation = [handler.mainGUIController loadAnimationFromCacheByName:_frameName withCount:_frameCount delay:0.075];
    
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
}

-(void)dealloc {
    [handler.mainGUIController removeAnimationInFrameCache:@"fire_drop_effect"];
    [super dealloc];
}


@end
